package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Examination;
import cn.wolfcode.crm.service.IExaminationService;
import cn.wolfcode.crm.util.ExaminationQueryObject;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("examination")
public class ExaminationController {

    @Autowired
    private IExaminationService examinationService;

    @RequestMapping("view")
    public String examination() {
        return "examination";
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult query(ExaminationQueryObject qo) {
        return examinationService.query(qo);
    }

    @RequestMapping("list")
    @ResponseBody
    public Object list() {
        return examinationService.selectAll();
    }
    
    
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Examination examination) {
        JSONResult result = new JSONResult();
        try {
            examinationService.saveOrUpdate(examination);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object delete(Long id){
        JSONResult result = new JSONResult();
        try {
            examinationService.delete(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.remark("删除失败");
        }
    }

    @RequestMapping("audit")
    @ResponseBody
    public Object audit(Long id){
        JSONResult result = new JSONResult();
        try {
            examinationService.audit(id);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result.remark("审核失败");
        }
    }

    @RequestMapping("exportXls")
    public void exportXls(ExaminationQueryObject qo, HttpServletResponse resp) throws IOException {
        String keyword = qo.getKeyword();
        byte[] keywordBytes = keyword.getBytes("ISO-8859-1");
        String s = new String(keywordBytes,"utf-8");
        qo.setKeyword(s);
        resp.setHeader("Content-Disposition","attachment;filename=examination.xls" );
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("day01");
        PageResult result = examinationService.query(qo);
        List<?> list = result.getRows();
        Row row = sheet.createRow(0);
        row.createCell(0).setCellValue("考试编号");
        row.createCell(1).setCellValue("考生");
        row.createCell(2).setCellValue("营销人员");
        row.createCell(3).setCellValue("QQ");
        row.createCell(4).setCellValue("电话");
        row.createCell(5).setCellValue("意向班级");
        row.createCell(6).setCellValue("考试时间");
        row.createCell(7).setCellValue("考试结果");
        row.createCell(8).setCellValue("备注");
        row.createCell(9).setCellValue("处理人");
        for (int i = 0; i <list.size() ; i++) {
            row = sheet.createRow(i + 1);
            Examination examination = (Examination) list.get(i);
            if(examination.getId() == null){
                row.createCell(0).setCellValue("");
            }else {

                row.createCell(0).setCellValue(examination.getId());
            }
            if(examination.getPotentialStudent().getName() == null){
                row.createCell(1).setCellValue("");
            } else {
                row.createCell(1).setCellValue(examination.getPotentialStudent().getName());
            }
            if(examination.getConsultant()==null){
                row.createCell(2).setCellValue("");
            } else {
                row.createCell(2).setCellValue(examination.getConsultant().getUsername());
            }
            if(examination.getQqNum() == null){
                row.createCell(3).setCellValue("");
            } else {
                row.createCell(3).setCellValue(examination.getQqNum());
            }
            if(examination.getTel() == null){
                row.createCell(4).setCellValue("");
            } else {
                row.createCell(4).setCellValue(examination.getTel());
            }
            if(examination.getGrade() == null){
                row.createCell(5).setCellValue("");
            } else {
                row.createCell(5).setCellValue(examination.getGrade().getClassName());
            }

            //设置输出日期的单元格格式为日期格式操作
            CreationHelper createHelper = wb.getCreationHelper();
            CellStyle cellStyle = wb.createCellStyle();
            //设置日期格式
            cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy/M/dd"));
            Cell cell = row.createCell(6);
            cell.setCellValue(examination.getTestTime());
            //把格式设置进去
            cell.setCellStyle(cellStyle);
            System.out.println(examination.getExaminationResult());
            if(examination.getExaminationResult() == null){
                row.createCell(7).setCellValue("");
            } else {
                row.createCell(7).setCellValue(examination.getExaminationResult());
            }
            if(examination.getRemark() == null){
                row.createCell(8).setCellValue("");
            } else {
                row.createCell(8).setCellValue(examination.getRemark());
            }

            if(examination.getInputUser() == null){
                row.createCell(9).setCellValue("");
            } else {
                row.createCell(9).setCellValue(examination.getInputUser().getUsername());
            }
        }
        wb.write(resp.getOutputStream());
    }
}
